package com.cloud.school.app.service.impl;

import com.cloud.school.app.entity.TbFriendship;
import com.cloud.school.app.mapstruct.IFriendshipConvertMapper;
import com.cloud.school.app.model.FriendshipModel;
import com.cloud.school.app.model.UserModel;
import com.cloud.school.app.repository.TbFriendshipMapper;
import com.cloud.school.app.service.FriendshipService;
import com.cloud.school.app.service.UserService;
import com.cloud.school.app.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName FriendshipServiceImpl
 * @Description TODO
 * @Author 36297
 * @Date 2018/12/14 0:53
 * @Version 1.0
 */
@Service
public class FriendshipServiceImpl implements FriendshipService {

    @Autowired
    private TbFriendshipMapper tbFriendshipMapper;
    @Autowired
    private IFriendshipConvertMapper friendshipConvertMapper;
    @Autowired
    private IdWorker friendshipIdWorker;
    @Autowired
    private UserService userService;

    @Override
    @Transactional
    public boolean save(FriendshipModel friendshipModel) {
        TbFriendship friendship = friendshipConvertMapper.modelToEntity(friendshipModel);
        friendship.setId(friendshipIdWorker.nextId());
        friendship.setCreateTime(System.currentTimeMillis());
        return tbFriendshipMapper.insertSelective(friendship) > 0;
    }

    @Override
    public boolean updateStatus(Long friendshipId, int status) {
        TbFriendship friendship = new TbFriendship();
        friendship.setId(friendshipId);
        friendship.setStatus(status);
        return tbFriendshipMapper.updateByPrimaryKeySelective(friendship) > 0;
    }

    @Override
    public List<FriendshipModel> selectByUidAndStatus(Long uid, int status) {
        TbFriendship friendship = new TbFriendship();
        friendship.setFirstUid(uid);
        friendship.setStatus(status);
        List<TbFriendship> result = tbFriendshipMapper.select(friendship);
        friendship = new TbFriendship();
        friendship.setStatus(status);
        friendship.setSecondUid(uid);
        List<TbFriendship> others = tbFriendshipMapper.select(friendship);
        Long tempUserId = null;
        for (TbFriendship f : others) {
            tempUserId = f.getSecondUid();
            f.setSecondUid(f.getFirstUid());
            f.setFirstUid(tempUserId);
        }
        result.addAll(others);
        return friendshipConvertMapper.entityToModel(result);
    }

    @Override
    public boolean isFriendShipExist(Long firstUid, Long secondUid) {
        TbFriendship friendship = new TbFriendship();
        friendship.setFirstUid(firstUid);
        friendship.setSecondUid(secondUid);
        boolean isExist = tbFriendshipMapper.selectCount(friendship) > 0;
        friendship = new TbFriendship();
        friendship.setFirstUid(secondUid);
        friendship.setSecondUid(firstUid);
        boolean isExistReverse = tbFriendshipMapper.selectCount(friendship) > 0;
        return isExist && isExistReverse;
    }

    private void appendUser(List<FriendshipModel> result) {
        List<Long> uids = result.stream().map(f -> f.getSecondUid()).filter(id -> id != null).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(uids)) {
            Map<Long, UserModel> userModelMap = userService.queryByIds(uids).stream().collect(Collectors.toMap(UserModel::getId, u -> u));
            result.forEach(f -> f.setFriendInfo(userModelMap.get(f.getSecondUid())));
        }
    }

    @Override
    public List<FriendshipModel> selectDetailByUidAndStatus(Long uid, int status) {
        List<FriendshipModel> result = selectByUidAndStatus(uid,status);
        appendUser(result);
        return result;
    }
}
